{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# KfServing Logger demo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We create a message dumper KNaive service to print out CloudEvents it receives:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[94mapiVersion\u001b[39;49;00m: serving.knative.dev/v1\r\n",
      "\u001b[94mkind\u001b[39;49;00m: Service\r\n",
      "\u001b[94mmetadata\u001b[39;49;00m:\r\n",
      "  \u001b[94mname\u001b[39;49;00m: message-dumper\r\n",
      "\u001b[94mspec\u001b[39;49;00m:\r\n",
      "  \u001b[94mtemplate\u001b[39;49;00m:\r\n",
      "    \u001b[94mspec\u001b[39;49;00m:\r\n",
      "      \u001b[94mcontainers\u001b[39;49;00m:\r\n",
      "      - \u001b[94mimage\u001b[39;49;00m: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display\r\n"
     ]
    }
   ],
   "source": [
    "!pygmentize message-dumper.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "service.serving.knative.dev/message-dumper created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl apply -f message-dumper.yaml -n default"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a Torchserve model with associated logger to push events to the message logger URL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "\u001b[94mapiVersion\u001b[39;49;00m: \u001b[33m\"\u001b[39;49;00m\u001b[33mserving.kubeflow.org/v1beta1\u001b[39;49;00m\u001b[33m\"\u001b[39;49;00m\n\u001b[94mkind\u001b[39;49;00m: \u001b[33m\"\u001b[39;49;00m\u001b[33mInferenceService\u001b[39;49;00m\u001b[33m\"\u001b[39;49;00m\n\u001b[94mmetadata\u001b[39;49;00m:\n  \u001b[94mname\u001b[39;49;00m: \u001b[33m\"\u001b[39;49;00m\u001b[33mtorchserve-custom\u001b[39;49;00m\u001b[33m\"\u001b[39;49;00m\n\u001b[94mspec\u001b[39;49;00m:\n  \u001b[94mpredictor\u001b[39;49;00m:\n    \u001b[94mminReplicas\u001b[39;49;00m: 1      \n    \u001b[94mlogger\u001b[39;49;00m:\n      \u001b[94murl\u001b[39;49;00m: http://message-dumper.default.svc.cluster.local\n      \u001b[94mmode\u001b[39;49;00m: all\n    \u001b[94mcontainers\u001b[39;49;00m:\n      - \u001b[94mimage\u001b[39;49;00m: {\u001b[31musername\u001b[39;49;00m}/torchserve:latest\n        \u001b[94mname\u001b[39;49;00m: torchserve-container\n"
     ]
    }
   ],
   "source": [
    "!pygmentize torchserve-logger.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "inferenceservice.serving.kubeflow.org/torchserve-custom created\n"
     ]
    }
   ],
   "source": [
    "!kubectl apply -f torchserve-logger.yaml -n kfserving-test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "a881f5a8c676a41edbccdb0a394a80d6-2069247558.us-west-2.elb.amazonaws.com\n"
     ]
    }
   ],
   "source": [
    "CLUSTER_IPS=!(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')\n",
    "CLUSTER_IP=CLUSTER_IPS[0]\n",
    "print(CLUSTER_IP)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "torchserve-custom.kfserving-test.a881f5a8c676a41edbccdb0a394a80d6-2069247558.us-west-2.elb.amazonaws.com\n"
     ]
    }
   ],
   "source": [
    "SERVICE_HOSTNAMES=!(kubectl get inferenceservice torchserve-custom -n kfserving-test -o jsonpath='{.status.url}' | cut -d \"/\" -f 3)\n",
    "SERVICE_HOSTNAME=SERVICE_HOSTNAMES[0]\n",
    "print(SERVICE_HOSTNAME)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "--2020-12-09 19:05:29--  https://raw.githubusercontent.com/pytorch/serve/master/examples/image_classifier/mnist/test_data/1.png\n",
      "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 199.232.252.133\n",
      "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|199.232.252.133|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 167 [image/png]\n",
      "Saving to: ‘1.png.2’\n",
      "\n",
      "1.png.2             100%[===================>]     167  --.-KB/s    in 0s      \n",
      "\n",
      "2020-12-09 19:05:30 (4.25 MB/s) - ‘1.png.2’ saved [167/167]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!wget https://raw.githubusercontent.com/pytorch/serve/master/examples/image_classifier/mnist/test_data/1.png"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torchserve-custom.kfserving-test.a881f5a8c676a41edbccdb0a394a80d6-2069247558.us-west-2.elb.amazonaws.com\r\n"
     ]
    }
   ],
   "source": [
    "!echo $SERVICE_HOSTNAME"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "*   Trying 52.37.128.207...\n",
      "* Connected to a881f5a8c676a41edbccdb0a394a80d6-2069247558.us-west-2.elb.amazonaws.com (52.37.128.207) port 80 (#0)\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "* We are completely uploaded and fine\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "* Connection #0 to host a881f5a8c676a41edbccdb0a394a80d6-2069247558.us-west-2.elb.amazonaws.com left intact\n",
      "1"
     ]
    }
   ],
   "source": [
    "!curl -v -H \"Host: $SERVICE_HOSTNAME\" http://$CLUSTER_IP/predictions/mnist -T 1.png"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "☁️  cloudevents.Event\nValidation: valid\nContext Attributes,\n  specversion: 1.0\n  type: org.kubeflow.serving.inference.request\n  source: http://localhost:9081/\n  id: 842e083d-ffb1-4f90-9649-14d291afa271\n  time: 2020-12-09T13:41:29.604504965Z\n  datacontenttype: application/json\nExtensions,\n  endpoint: \n  inferenceservicename: torchserve-custom\n  namespace: kfserving-test\n  traceparent: 00-c404939670665fd4630adc993d99fe43-a53901ad0192f760-00\nData,\n\n\u001a\nIHDR\u0000\u0000\u0000\u001c\u0000\u0000\u0000\u0000\u0000\u0000\u0000Wf�H\u0000\u0000\u0000nIDATx�c`\u0018�`��$�r�o�n�)��\u0006�$��߻��e��q�'y\u0018��G?<�rpJ����[2\u0017�N��\u001ap����?�%=����-���\u0004\u001aHj���_\u0019�l����%U�I\n����)I-\u0000\u0000�J4ԩ�\u0000\u0000\u0000\u0000IEND�B`�\n☁️  cloudevents.Event\nValidation: valid\nContext Attributes,\n  specversion: 1.0\n  type: org.kubeflow.serving.inference.response\n  source: http://localhost:9081/\n  id: 842e083d-ffb1-4f90-9649-14d291afa271\n  time: 2020-12-09T13:41:34.829648499Z\n  datacontenttype: application/json\nExtensions,\n  endpoint: \n  inferenceservicename: torchserve-custom\n  namespace: kfserving-test\n  traceparent: 00-396af54bc5b3decdb7dc0afc27022b54-1f385ad3861c25b4-00\nData,\n  1\n"
     ]
    }
   ],
   "source": [
    "!kubectl logs $(kubectl get pod -l serving.knative.dev/configuration=message-dumper -n default -o jsonpath='{.items[0].metadata.name}') -c user-container "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "inferenceservice.serving.kubeflow.org \"torchserve-custom\" deleted\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f torchserve-logger.yaml -n kfserving-test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "service.serving.knative.dev \"message-dumper\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f message-dumper.yaml -n kfserving-test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}